package com.hncic.tx.center.mvc;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.hncic.tx.center.common.model.Account;
import com.hncic.tx.center.common.util.Constant;
import com.hncic.tx.center.common.vo.JsonData;
import com.hncic.tx.center.common.vo.JsonData0;
import com.hncic.tx.center.mvc.util.TxSession;
import com.hncic.tx.center.service.UserServiceImpl;

/**
 * @author smj E-mail: 463136320@qq.com
 * @created 2017年8月18日 上午10:24:42
 * @since jdk1.8 
 * @version 1.0
 * @describe 
 */
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController {

	private @Autowired UserServiceImpl userServiceImpl;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public JsonData0 login(@RequestParam(required = true) String account, //
			@RequestParam(required = true) String passWord, //
			HttpSession session) {
		JsonData0 result = new JsonData0();

		Account account2 = userServiceImpl.queryAccount(account);
		if (account2 == null) {
			result.setMsg("账号不存在!!");
		} else if (account2.getDel())
			result.setMsg("账号已删除!!");
		else if (!Account.State.normal.equals(account2.getState()))
			result.setMsg("账号状态非可用!!");
		else if (!passWord.equals(account2.getPassWord()))
			result.setMsg("密码错误!!");
		else {// 登录成功
			TxSession sess = new TxSession();
			sess.setAccount(account2.getAccount());
			session.setAttribute(Constant.TxSessKey, sess);
		}
		return result;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonData<Date> update(@SessionAttribute(Constant.TxSessKey) TxSession txSess) {
		JsonData<Date> result = new JsonData<>();
		if (txSess == null) {

		}
		result.setData(new Date());
		return result;
	}

	@RequestMapping(value = "/out", method = RequestMethod.POST)
	public JsonData0 login(@RequestParam String account, HttpSession session) {
		session.removeAttribute(Constant.TxSessKey);
		return JsonData0.Succ;
	}
}
